<template>
  <el-dialog title="提示" :visible.sync="visible" width="30%" :before-close="handleClose">
    <el-form
      :model="formData"
      :rules="rules"
      ref="formData"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="类型" prop="type">
        <el-radio-group v-model="formData.type">
          <el-radio :label="1">目录</el-radio>
          <el-radio :label="2">菜单</el-radio>
          <el-radio :label="3" v-if="formData.parentId!==0">按钮</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input v-model="formData.name"></el-input>
      </el-form-item>
      <el-form-item label="权限标识" prop="code">
        <el-input v-model="formData.code"></el-input>
      </el-form-item>
      <!-- 3是按钮 按钮不显示 -->
      <el-form-item label="请求地址" prop="url" v-if="formData.type != 3">
        <el-input v-model="formData.url"></el-input>
      </el-form-item>
      <el-form-item label="图标" prop="icon" v-if="formData.type != 3">
        <el-input v-model="formData.icon"></el-input>
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input-number v-model="formData.sort" @change="handleChange"></el-input-number>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input type="textarea" v-model="formData.remark"></el-input>
      </el-form-item>
      <el-form-item label="操作">
        <el-button type="primary" @click="submitForm('formData')">确定</el-button>
        <el-button @click="handleClose('formData')">取消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>
<script>
import menu from "@/api/menu";

export default {
  props: {
    visible: {
      //弹出框
      type: Boolean,
      default: false
    },
    title: {
      type: String,
      default: ""
    },
    // 下拉分类名称

    // 表单数据
    formData: {
      type: Object,
      default: {}
    },
    remoteClose: Function
  },

  data() {
    return {
      rules: {
        type: [{ required: true, message: "请输入类型", trigger: "blur" }],
        name: [{ required: true, message: "请输入名称", trigger: "blur" }],
        code: [{ required: true, message: "请输入权限标识", trigger: "blur" }],
        url: [{ required: true, message: "请输入请求地址", trigger: "blur" }],
        icon: [{ required: true, message: "请输入图标", trigger: "blur" }],
        sort: [{ required: true, message: "请输入排序", trigger: "blur" }],
        remark: [{ required: true, message: "请输入备注", trigger: "blur" }]
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        //   console.log(valid)// false或true
        if (valid) {
          //   id=0是新增的一级菜单 否则是新增的某菜单下的子菜单
          if (this.formData.type == 3) {
            (this.formData.url = ""), (this.formData.icon = "");
          }
          this.submitData();
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    //   关闭窗口
    handleClose() {
      this.remoteClose();
      this.$refs["formData"].resetFields();
    },
    handleChange() {},

    async submitData() {
      let res = null;
      // 编辑
      if (this.formData.id) {
        res = await menu.update(this.formData);
      } else {
        // 新增
        res = await menu.add(this.formData);
      }
      if (res.code == 20000) {
        // 提交成功, 关闭窗口, 刷新列表
        this.$message({
          message: "保存成功",
          type: "success"
        });
        this.handleClose();
      } else {
        this.$message({
          message: "保存失败",
          type: "error"
        });
      }
    }
  }
};
</script>